package com.magic.module.sys.controller;

/*
查询用户新增、修改、删除、查询等操作的controller
 */

import cn.hutool.core.util.StrUtil;
import com.magic.framework.aop.repeatedSubmit.RepeatSubmitLimit;
import com.magic.framework.cache.MagicLocalCache;
import com.magic.framework.constant.CacheConstants;
import com.magic.framework.model.AjaxResult;
import com.magic.module.sys.entity.SysUserEntity;
import com.magic.module.sys.mapper.SysUserMapper;
import com.magic.module.sys.model.UserAndOrderModel;
import com.magic.module.sys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理")
public class SysUserController {
    private MagicLocalCache<Integer> cache = new MagicLocalCache<Integer>();

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ApiOperation(value = "固定信息", notes = "返回固定信息")
    public String getUserInfo() {
        return "ALL user info 1";
    }

    @RequestMapping(value = "/orderRepeatSubmit", method = RequestMethod.POST)
    @ApiOperation(value = "订单重复提交", notes = "根据参数orderId和userId判断订单是否重复提交")
    @RepeatSubmitLimit(businessKey = "id", businessParam = "#dictType")
    public AjaxResult testOrderRepeatSubmit(@RequestParam String remark, @RequestParam String id, @RequestParam String dictType) {

        if (StrUtil.isEmpty(remark) || StrUtil.isEmpty(id)) {
            return AjaxResult.error("订单ID不能为空");
        }

        System.out.println("从数据库查询用户信息...");
        System.out.println("remark:" + remark + " id:" + id);

        SysUserEntity sysUserEntity = sysUserMapper.selectById(id);

        SysUserEntity user = new SysUserEntity();
        user.setName("order");
        user.setRemark("123");
        sysUserService.save(user);

        return AjaxResult.success(sysUserEntity);
    }


    @PostMapping("/getUser")
    @ApiOperation(value = "获取用户信息", notes = "表单提交验证")
    public AjaxResult getUser(@Valid @RequestBody UserAndOrderModel userAndOrderModel, BindingResult bindingResult) {
        System.out.println("Received UserAndOrderModel: " + userAndOrderModel);

        // 如果有错误提示信息
        if (bindingResult.hasErrors()) {
            return AjaxResult.error(bindingResult.getFieldError().getDefaultMessage());
        }

        Long id = userAndOrderModel.getUserId();

        UserAndOrderModel u =  sysUserService.getUnionUserInfo(id);

        System.out.println(u);

        if(u==null){
            return AjaxResult.error(8888,"用户不存在");
        }

        return AjaxResult.success(u);
    }

    @RequestMapping(value = "/getUser2", method = RequestMethod.POST)
    @Cacheable(value = CacheConstants.DICT_DETAILS, key = "#user.id")
    public AjaxResult getUser2(SysUserEntity user) {
        SysUserEntity sysUserEntity =  sysUserService.lambdaQuery().eq(SysUserEntity::getId,user.getId()).one();

        List<SysUserEntity> sysUserEntityList =  sysUserMapper.selectList(null);
        System.out.println(sysUserEntityList.size());

        if(sysUserEntity ==null){
            return AjaxResult.error("用户不存在");
        }

        return AjaxResult.success(sysUserEntity);
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除字典")
    @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
    public AjaxResult delete(@RequestBody Long id) {
        sysUserMapper.deleteById(id);
        return AjaxResult.success("删除成功");
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public AjaxResult addUser(SysUserEntity user){
        user.setName("管理员");
        sysUserService.save(user);
        return AjaxResult.success();
    }

}
